Merge "rdbms: make LBFactory close/rollback dangling handles like LoadBalancer"
[lhc/web/wiklou.git] / includes / libs / rdbms / lbfactory / LBFactorySimple.php
1 <?php
2 /**
3 * Generator of database load balancing objects.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 namespace Wikimedia\Rdbms;
25
26 use InvalidArgumentException;
27
28 /**
29 * A simple single-master LBFactory that gets its configuration from the b/c globals
30 */
31 class LBFactorySimple extends LBFactory {
32 /** @var LoadBalancer */
33 private $mainLB;
34 /** @var LoadBalancer[] */
35 private $extLBs = [];
36
37 /** @var array[] Map of (server index => server config) */
38 private $servers = [];
39 /** @var array[] Map of (cluster => (server index => server config)) */
40 private $externalClusters = [];
41
42 /** @var string */
43 private $loadMonitorClass;
44
45 /**
46 * @see LBFactory::__construct()
47 * @param array $conf Parameters of LBFactory::__construct() as well as:
48 * - servers : list of server configuration maps to Database::factory().
49 * Additionally, the server maps should have a 'load' key, which is used to decide
50 * how often clients connect to one server verses the others. A 'max lag' key should
51 * also be set on server maps, indicating how stale the data can be before the load
52 * balancer tries to avoid using it. The map can have 'is static' set to disable blocking
53 * replication sync checks (intended for archive servers with unchanging data).
54 * - externalClusters : map of cluster names to server arrays. The servers arrays have the
55 * same format as "servers" above.
56 */
57 public function __construct( array $conf ) {
58 parent::__construct( $conf );
59
60 $this->servers = $conf['servers'] ?? [];
61 $this->externalClusters = $conf['externalClusters'] ?? [];
62 $this->loadMonitorClass = $conf['loadMonitorClass'] ?? 'LoadMonitor';
63 }
64
65 public function newMainLB( $domain = false ) {
66 return $this->newLoadBalancer( $this->servers );
67 }
68
69 public function getMainLB( $domain = false ) {
70 if ( !$this->mainLB ) {
71 $this->mainLB = $this->newMainLB( $domain );
72 }
73
74 return $this->mainLB;
75 }
76
77 public function newExternalLB( $cluster ) {
78 if ( !isset( $this->externalClusters[$cluster] ) ) {
79 throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"." );
80 }
81
82 return $this->newLoadBalancer( $this->externalClusters[$cluster] );
83 }
84
85 public function getExternalLB( $cluster ) {
86 if ( !isset( $this->extLBs[$cluster] ) ) {
87 $this->extLBs[$cluster] = $this->newExternalLB( $cluster );
88 }
89
90 return $this->extLBs[$cluster];
91 }
92
93 public function getAllMainLBs() {
94 return [ 'DEFAULT' => $this->getMainLB() ];
95 }
96
97 public function getAllExternalLBs() {
98 $lbs = [];
99 foreach ( $this->externalClusters as $cluster => $unused ) {
100 $lbs[$cluster] = $this->getExternalLB( $cluster );
101 }
102
103 return $lbs;
104 }
105
106 private function newLoadBalancer( array $servers ) {
107 $lb = new LoadBalancer( array_merge(
108 $this->baseLoadBalancerParams(),
109 [
110 'servers' => $servers,
111 'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
112 ]
113 ) );
114 $this->initLoadBalancer( $lb );
115
116 return $lb;
117 }
118
119 public function forEachLB( $callback, array $params = [] ) {
120 if ( isset( $this->mainLB ) ) {
121 $callback( $this->mainLB, ...$params );
122 }
123 foreach ( $this->extLBs as $lb ) {
124 $callback( $lb, ...$params );
125 }
126 }
127 }